<?php
namespace app\admin\controller;
use app\admin\controller\Base;
use think\Controller;
use think\Db;
use think\Request;
use think\facade\Env;
use think\facade\Session;
// use app\admin\Model\Gkdata;
class Admin extends Base
{
    protected $beforeActionList=[
        'first'
    ];
    public function first(){
        $adgrade=Session::get('adgrade');
        if($adgrade !='超级管理员'){
            return $this->error("对不起您没有权限，如有需求请联系超级管理员",'index/index');
        }
    }
    public function index($page=1)
    {
        $data=Db::table('admin')->page($page,5)->select();
        $num=Db::table('admin')->count('adid');
        $pagenum=ceil($num/5);
        // $typedata=Db::name('newstype')->select();
        $this->assign('cpage',$page);
        // $this->assign('typedata',$typedata);
        $this->assign('data',$data);
        $this->assign('pagenum',$pagenum);
        return $this->fetch();
    }
    // 进入添加管理员页面
    public function addpage(){
        return $this->fetch();
    }
    // 添加管理员
    public  function addad(){
        // $adxm1=Session::get('adxm');
        $request=new request();
        $adxm = $request->post("adxm");
        $data=Db::name('admin')->where('adxm',$adxm)->find();
        if(empty($data)){
            $phone = $request->post("phone");
            $password = $request->post("password");
            $nc = $request->post("nc");
            $adgrade = $request->post("adgrade");
            $sex = $request->post("sex");
            // 实例化上传图片的对象
            $upimgobj=controller('Common');
            $image_error = $_FILES['adimg']['error'];
            if ($image_error == 4) {
                $adimg='';
            }else{
                $file=$request->file('adimg');
                $adimg=$upimgobj->upaction1($file);          
            }
            $data1=[
                "adxm"=>$adxm,
                "adpd"=>$password,
                "adphone"=>$phone,
                "adname"=>$nc,
                "adgrade"=>$adgrade,
                "adimg"=>$adimg,
                "adsex"=>$sex
            ];
            $re=Db::name('admin')->insert($data1);
            if($re){
                $this->success('新增管理员成功', 'admin/index');
            }else{
                return $this->error('新增管理员失败');
            }
            
            
        }else{
            // return $this->error('用户名已存在，请重新输入！');
            var_dump($data);
        }
    }
    // 进入更改
    public function editpage(){
        $request=new request();
        $adid= intval($request->get("id"));
        $data=Db::name('admin')->where('adid',$adid)->find();       
        $this->assign('data',$data);
        return $this->fetch();
    }
    // 更改管理员
    public  function editad(){
        $request=new request();
        $data1=[];
        $adid=$request->post('adid');
        $data1['adid']=$adid;
        $data=Db::name('admin')->where('adid',$adid)->find();
        $adxm = $request->post("adxm");
        if($adxm!=$data['adxm']){
            $data1['adxm']=$adxm;
        }
        $adphone = $request->post("phone");
        if($adphone!=$data['adphone']){
            $data1['adphone']=$adphone;
        }
        $adpd = $request->post("password");
        if($adpd!=$data['adpd']){
            $data1['adpd']=$adpd;
        } 
        $adname = $request->post("nc");
        if($adname!=$data['adname']){
            $data1['adname']=$adname;
        }
        $adgrade = $request->post("adgrade");
        if($adgrade!=$data['adgrade']){
            $data1['adgrade']=$adgrade;
        }
        $adsex = $request->post("adsex");
        if($adsex!=$data['adsex']){
            $data1['adsex']=$adsex;
        }
        // 实例化上传图片的对象
        $upimgobj=controller('Common');
        $image_error = $_FILES['adimg']['error'];
        if ($image_error != 4) {
            $file=$request->file('adimg');
            $adimg=$upimgobj->upaction1($file);
            $data1['adimg']=$adimg;
        }
        
        $da=Db::name('admin')->update($data1);
        if($da){
            $this->success('更新管理员成功', 'admin/index');
        }else{
            return $this->error('更新管理员失败');
        }

    }
    // 删除用户
    public function deladmin()
    {

        $request=new request();
        $adid=$request->get("id");
        $da=Db::name('admin')->delete($adid);
        if($da){
            $this->success('删除成功', 'admin/index');
        }
        else{
            return $this->error('删除失败');
        }
    }




}
